<template>
  <div class="box">
    <!-- 输入表单 -->
    <el-form ref="elForm" :model="formData" :rules="rules" size="small" label-width="68px">

      <el-form-item label="学校名称" prop="schoolName" size="small">
        <el-input v-model="formData.schoolName" placeholder="请输入学校名称">
        </el-input>
      </el-form-item>

      <el-form-item label="学校性质" prop="schoolQuality" size="small">
        <el-input v-model="formData.schoolQuality" placeholder="请输入学校性质">
        </el-input>
      </el-form-item>

      <el-form-item label="状态" prop="isDeleted" size="small">
        <el-select v-model="formData.isDeleted" placeholder="请选择状态">
          <el-option v-for="(item, index) in isDeletedOptions" :key="index" :label="item.label" :value="item.value"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否推荐" prop="isRecommend" size="small">
        <el-select v-model="formData.isRecommend" placeholder="请选择是否推荐">
          <el-option v-for="(item, index) in isRecommendOptions" :key="index" :label="item.label" :value="item.value"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item size="mini">
        <el-button type="primary" @click="submitForm" icon="el-icon-search">提交</el-button>
        <el-button @click="resetForm" icon="el-icon-refresh">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增按钮 -->
    <el-button size="mini" type="primary" plain icon="el-icon-plus" style="display:block ; margin-bottom: 10px;"
      @click="addSchool">新增
    </el-button>
    <!-- 列表栏 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="schoolId" label="id" width="180">
      </el-table-column>
      <el-table-column prop="schoolName" label="学校名称" width="180">
      </el-table-column>
      <el-table-column prop="schoolQuality" label="学校性质">
      </el-table-column>
      <el-table-column prop="schoolPhone" label="院校电话">
      </el-table-column>
      <el-table-column prop="schoolAddress" label="院校住址">
      </el-table-column>
      <el-table-column prop="isDeleted" label="状态">
        <template slot-scope="scope">
          {{ scope.row.isDeleted ? "启用" : "禁用" }}
        </template>
      </el-table-column>
      <el-table-column prop="isRecommend" label="是否推荐">
        <template slot-scope="scope">
          {{ scope.row.isRecommend ? "推荐" : "不推荐" }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button @click="updateSchoolButton(scope.row)" type="text" size="small" icon="el-icon-edit">修改</el-button>
          <el-button @click="deleteSchool(scope.row)" type="text" size="small" icon="el-icon-delete">删除</el-button>
          <el-button @click="modifyisDeleted(scope.row)" type="text" size="small" icon="el-icon-close"> {{
              scope.row.isDeleted ? "禁用" : "启用"
          }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" v-if="dialogVisible">
      <edit :schoolList="schoolList" :flag="flag" @update="updateValue"> </edit>
    </el-dialog>

    <pagination v-show="total > 0" :total="total" :page.sync="formData.pageNum" :limit.sync="formData.pageSize"
      @pagination="getList" />
  </div>
</template>
<script>
import { listschool, delschool, updateschool } from "../../../api/fetv/school";
import edit from "./edit.vue";
export default {

  components: {
    edit
  },
  props: [],
  data() {
    return {
      //判断表单是修改还是添加
      flag: 0,
      // 点击修改时的单个学校数据
      schoolList: {},
      total: 0,
      // 弹出框的显示和隐藏
      dialogVisible: false,
      // 表单
      formData: {
        pageNum: 1,
        pageSize: 10,
        schoolName: undefined,
        schoolQuality: undefined,
        isDeleted: undefined,
        isRecommend: undefined,
      },
      rules: {
        schoolName: [],
        schoolQuality: [],
        isDeleted: [],
        isRecommend: [],
      },
      isDeletedOptions: [{
        "label": "启用",
        "value": 1
      }, {
        "label": "禁用",
        "value": 0
      }],
      isRecommendOptions: [{
        "label": "推荐",
        "value": 1
      }, {
        "label": "不推荐",
        "value": 0
      }],
      // 列表数据
      tableData: []
    }
  },
  computed: {},
  watch: {},
  created() {
    // 初始化列表
    this.getList()
  },
  mounted() { },
  methods: {
    // 子组件传值关闭弹窗
    updateValue(newValue) {
      this.dialogVisible = newValue;
      this.schoolList = {}
      this.getList()
    },
    // 修改状态
    async modifyisDeleted(e) {
      let list = e
      if (list.isDeleted === 1) {
        list.isDeleted = 0
      } else {
        list.isDeleted = 1
      }
      const res = await updateschool(list)
      if (res.code === 200) {
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      }
      this.getList()
    },
    addSchool() {
      // 添加学院
      this.dialogVisible = true
      this.flag = 1

    },
    // 删除学院
    async deleteSchool(e) {
      console.log(e.schoolId);
      const res = await delschool(e.schoolId)
      console.log(res.code);
      if (res.code === 200) {
        this.$message({
          message: '删除成功',
          type: 'success'
        });
      }
      this.getList()

    },
    // 关闭弹出框的函数
    handleClose(done) {
      this.schoolList = {}
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    // 提交表单查询
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // console.log(valid);
        this.getList()
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    // 获取表单数据
    async getList() {
      try {
        const res = await listschool(this.formData)
        this.tableData = res.rows
        this.total = res.total
      } catch (error) {
        console.log(error)
      }
    },
    //修改
    updateSchoolButton(e) {
      this.dialogVisible = true
      this.flag = 0
      this.schoolList = JSON.parse(JSON.stringify(e));
      console.log(this.schoolList, "点击时候的数据");
    }
  }
}

</script>
<style lang="scss" scoped>
.box {
  padding: 20px;
}

::v-deep .el-form-item--mini.el-form-item {
  margin-left: -52px;
  display: inline-block;
}

::v-deep .el-form-item--small.el-form-item {
  display: inline-block;
  margin-right: 12px;
}

::v-deep .upload-demo {
  margin-bottom: 20px;
}
</style>
